package controller;



import com.fasterxml.jackson.databind.ObjectMapper;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {


    //使用这个数据类型来将数据转成json格式
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //数据显示格式
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");


        //1.接收客户端传来的数据  用户名  密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //2.插入数据库
        UserDao userDao = new UserDao();
        int flag = userDao.RegisterUser(username,password);
//        System.out.println("注册成功");
//        resp.getWriter().write("注册成功");
        //3.返回信息表示是否注册成功
        if (flag == 1){
            System.out.println("注册成功");
            resp.getWriter().write("1");
        }else if(flag == 2){
            System.out.println("该用户已存在");
            resp.getWriter().write("2");
        }
        else {
            System.out.println("注册失败");
            resp.getWriter().write("-1");
        }




    }
}
